﻿Imports System.Diagnostics
Imports System.Drawing
Imports System.Runtime.InteropServices
Imports System.Text


Public Class PCHandler









#Region "Memory Status"
    Public Shared Sub GetPhysicalMemory(ByRef availPhysMem As Long, ByRef totalPhysMem As Long)
        Dim bytes = My.Computer.Info.AvailablePhysicalMemory
        Dim kb = bytes / 1024
        availPhysMem = kb / 1024
        bytes = My.Computer.Info.TotalPhysicalMemory
        kb = bytes / 1024
        totalPhysMem = kb / 1024
    End Sub

    Public Shared Sub GetVirtualMemory(ByRef availVirtMem As Long, ByRef totalVirtMem As Long)
        Dim bytes = My.Computer.Info.AvailableVirtualMemory
        Dim kb = bytes / 1024
        availVirtMem = kb / 1024
        bytes = My.Computer.Info.TotalVirtualMemory
        kb = bytes / 1024
        totalVirtMem = kb / 1024
    End Sub

#End Region

#Region "Power State"
    Public Shared Sub BeginShutdown()
        Process.Start("shutdown", "-s -f -t 40")
    End Sub

    Public Shared Sub CancelShutdown()
        Process.Start("shutdown", "-a")
    End Sub

    Public Shared Sub Restart()
        Process.Start("shutdown", "-r -f -t 40")
    End Sub

    Public Shared Sub Hibernate()
        Process.Start("ShutDown", "/h")
    End Sub

    Public Shared Sub Standby()
        Process.Start("rundll32.exe", "powrprof.dll,SetSuspendState 0,1,0")
    End Sub
    'Public Shared Function GetPowerState() As String
    '    Dim builder As New StringBuilder



    '    Return builder.ToString()
    'End Function
#End Region

#Region "Processes"
    Public Shared Function GetProcessList() As String
        Dim builder As New StringBuilder

        For Each proc In Process.GetProcesses
            builder.AppendLine(String.Format("{0} => {1}", proc.Id, proc.ProcessName))
        Next

        'Dim filePath As String = String.Format("{0}\ListOfProcesses", System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData))
        'Dim i As Integer = 1
        'While File.Exists(filePath)
        '    filePath = String.Format("{0} ({1})", filePath, i.ToString())
        '    i += 1
        'End While
        'filePath += ".txt"

        'Using ioFile As New StreamWriter(filePath)
        '    For Each proc In Process.GetProcesses

        '    Next
        'End Using

        'Return filePath
        Return builder.ToString
    End Function
#End Region

#Region "Screen Brightness"
    <DllImport("user32.dll")> _
    Public Shared Function SetWindowLong(ByVal window As IntPtr, ByVal index As Integer, ByVal value As Integer) As Integer
    End Function
    <DllImport("user32.dll")> _
    Public Shared Function GetWindowLong(ByVal window As IntPtr, ByVal index As Integer) As Integer
    End Function

    Public Shared Sub DarkenScreen()

    End Sub
#End Region

#Region "Screenshot"
    Public Shared Function GetScreenshot() As Byte()
        Dim scr As New ScreenCapture
        Return scr.CaptureScreenAsPNG()
    End Function
#End Region

#Region "Screen Saver"
    Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Integer, ByVal uParam As Integer, ByVal lpvParam As Integer, ByVal fuWinIni As Integer) As Integer

    Public Function ToggleScreenSaver(ByRef Active As Boolean) As Boolean
        'DebugPrintHelper("now in ToggleScreenSaverActive")
        Dim lActiveFlag As Integer
        Dim retvaL As Integer
        lActiveFlag = IIf(Active, 1, 0)
        retvaL = SystemParametersInfo(17, lActiveFlag, 0, 0)
        ToggleScreenSaver = retvaL > 0
    End Function
#End Region

#Region "Volume"
    <DllImport("user32.dll")> _
    Private Shared Function SendMessageW(ByVal hWnd As IntPtr, _
           ByVal Msg As Integer, ByVal wParam As IntPtr, _
           ByVal lParam As IntPtr) As IntPtr
    End Function

    Private Declare Function mixerOpen Lib "winmm.dll" _
    (ByRef phmx As Integer, <MarshalAs(UnmanagedType.U4)> ByVal uMxId As Integer, ByVal dwCallback As Integer, ByVal dwInstance As Integer, ByVal fdwOpen As Integer) As Integer

    Public Shared Sub MuteVolume()

    End Sub

    Public Shared Sub UnmuteVolume()

    End Sub

    Public Shared Sub IncreaseVolume(ByVal amount As Integer)

    End Sub

    Public Shared Sub DecreaseVolume(ByVal amount As Integer)

    End Sub
#End Region

#Region "System Info"
    Public Shared Function GetSystemInfo() As String
        Dim builder As New StringBuilder

        builder.AppendLine(String.Format("{0} - {1}", Environment.OSVersion, Environment.OSVersion.Version.Build))
        builder.AppendLine(Environment.UserName)
        builder.AppendLine(Environment.UserDomainName)
        builder.AppendLine(Environment.MachineName)

        Return builder.ToString
    End Function
#End Region

#Region "User Session"
    Public Shared Sub Logoff()
        Process.Start("shutdown", "-l -f -t 40")
    End Sub

    Public Shared Sub LockComputer()
        Process.Start("rundll32.exe", "user32.dll,LockWorkStation")
    End Sub
#End Region



End Class
